import java.util.List;

import net.sf.json.JSONArray;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.stat.SessionStatistics;
  
import Entity.Room;
import tool.HibernateSessionFactory;

public class DAORoom extends DAOAbstract{
	

	public List<Room> queryRoomInfo(int roomid){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from Room where roomid="+roomid);
			List<Room> list = query.list();
			
			session.getTransaction().commit();
			System.out.println(list);
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	public List<Room> queryRoomInfo(){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from Room order by roomtype");
			List<Room> list = query.list();
			
			session.getTransaction().commit();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	public boolean addUser(int roomid){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Room room = (Room) session.get(Room.class, roomid);
			room.setRoomstatus(1);
			session.update(room);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}
	

	public boolean updateRoomInfo(Room room){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			session.update(room);
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}
	}
	

	public List<Room> queryAllRoomForEmpty(){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			System.out.println("123");
			Query query = session.createQuery("from Room where roomstatus = 0 order by roomtype");
			System.out.println("12");
			List<Room> list = query.list();
			System.out.println(list);
			session.getTransaction().commit();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	

	public List<Room> queryRoomForEmptyByType(int roomtype){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Query query = session.createQuery("from Room where roomstatus = 0 and roomtype = ?").setInteger(0, roomtype);
			List<Room> list = query.list();
			session.getTransaction().commit();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	
	public int checkOut(int roomid){
		Session session = HibernateSessionFactory.getSession();
		session.clear();
		try {
			session.beginTransaction();
			Room room = (Room) session.get(Room.class, roomid);
			room.setRoomstatus(0);
			session.update(room);
			session.getTransaction().commit();
			return room.getRoomprice();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			session.getTransaction().rollback();
			return -1;
		}
	}
}
